package com.Dao;
import java.sql.Connection;


import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.pojo.User;
public class UserDao {
	
	private int shopCount;
	private int shopCountX;
	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {}
	}
	private Connection getConn() throws SQLException {
		String url = "jdbc:mysql://localhost:3306/hi_db" + "?useUnicode=true&characterEncoding=UTF-8";
		String username = "root";
		String password = "15723";
		Connection conn = DriverManager.getConnection(url, username, password);
		return conn;
	}

	
	public boolean findUserByBC(
			User user) throws SQLException {
		
		Connection conn = getConn();
		
		Statement st = conn.createStatement();
		
		String str = "SELECT * FROM user WHERE"
				+ " Uphone='"+user.getUphone()+"'";
		
		ResultSet rs = st.executeQuery(str);
		
		if(rs.next()) {
			rs.close();
			st.close();
			conn.close();
			return true;
		}
		rs.close();
		st.close();
		conn.close();
		return false;
		
	}
	
	public Integer save(User user) throws SQLException {
		
		Connection conn = getConn();
		
		Statement st = conn.createStatement();
		
		String sql = "INSERT INTO user(Uname,Uphone,Uemail,Upsw,Urpsw) "
				+ "VALUES('"+user.getUname()+"','"+user.getUphone()+"','"+user.getUemail()+"','"+user.getUpsw()+"','"+user.getUrpsw()+"')";
		Integer result = st.executeUpdate(sql);
		
		st.close();
		
		conn.close();
		
		return result;
		
	}
	
	public User findUserByUser(User user) throws SQLException {
		//1.��������
		Connection conn = getConn();//��������ķ���
		//2.����ִ�п�
		Statement st = conn.createStatement();
		//3.��дSQL����
		String sql = "SELECT * FROM user WHERE "
			+ "Uphone='"+user.getUphone()+"' AND "
			+ "Upsw='"+user.getUpsw()+"'";
		//4��ִ��SQL
		ResultSet rs = st.executeQuery(sql);
		if(rs.next()) {//Ϊtrue����������
			User x = new User(//��ȡ���������User����
				rs.getInt("Uid"),
				rs.getString("Uname"),
				rs.getString("Uphone"),
				rs.getString("Uemail"),
				rs.getString("Upsw"),
				rs.getString("Urpsw"),
				rs.getString("Uage"),
				rs.getString("Uhp"),
				rs.getString("Usex"),
				rs.getString("Uhome"),
				rs.getString("Uexplain"),
				rs.getString("Ujst"));
			rs.close();//�رս����
			st.close();//�ر�ִ�п�
			conn.close();//�ر�����
			return x;//���ز�ѯ�����û���Ϣ
		}
		else {
			rs.close();//�رս����
			st.close();//�ر�ִ�п�
			conn.close();//�ر�����
			return null;
		}
	}
	public String findpassword(Integer uid) throws SQLException {
		//1.创建连接
		Connection conn = getConn();//调用上面的方法
		//2.创建执行块
		Statement st = conn.createStatement();
		//3.编写SQL代码
		String sql = "SELECT * FROM user WHERE "
			+ "Uid="+uid+"";
		//4、执行SQL
		ResultSet rs = st.executeQuery(sql);
		if(rs.next()) {//为true代表有数据
			String pasd =rs.getString("uhp");
					
			rs.close();//关闭结果集
			st.close();//关闭执行块
			conn.close();//关闭连接
			return pasd;//返回查询到的用户信息
		}
		else {
			rs.close();//关闭结果集
			st.close();//关闭执行块
			conn.close();//关闭连接
			return null;
		}
	}
	public User findUserByUsery(String Uphone ) throws SQLException {
		//1.��������
		Connection conn = getConn();//��������ķ���
		//2.����ִ�п�
		Statement st = conn.createStatement();
		//3.��дSQL����
		String sql = "SELECT * FROM user WHERE "
			+ "Uphone='"+Uphone+"'";
		//4��ִ��SQL
		ResultSet rs = st.executeQuery(sql);
		if(rs.next()) {//Ϊtrue����������
			User x = new User(//��ȡ���������User����
				rs.getInt("Uid"),
				rs.getString("Uname"),
				rs.getString("Uphone"),
				rs.getString("Uemail"),
				rs.getString("Upsw"),
				rs.getString("Urpsw"),
				rs.getString("Uage"),
				rs.getString("Uhp"),
				rs.getString("Usex"),
				rs.getString("Uhome"),
				rs.getString("Uexplain"),
				rs.getString("Ujst"));
			rs.close();//�رս����
			st.close();//�ر�ִ�п�
			conn.close();//�ر�����
			return x;//���ز�ѯ�����û���Ϣ
		}
		else {
			rs.close();//�رս����
			st.close();//�ر�ִ�п�
			conn.close();//�ر�����
			return null;
		}
	}
	public User findUserByUserx(User user) throws SQLException {
		//1.��������
		Connection conn = getConn();//��������ķ���
		//2.����ִ�п�
		Statement st = conn.createStatement();
		//3.��дSQL����
		String sql = "SELECT * FROM user WHERE "
			+ "Uid="+user.getUid()+"";
		//4��ִ��SQL
		ResultSet rs = st.executeQuery(sql);
		if(rs.next()) {//Ϊtrue����������
			User x = new User(//��ȡ���������User����
				rs.getInt("Uid"),
				rs.getString("Uname"),
				rs.getString("Uphone"),
				rs.getString("Uemail"),
				rs.getString("Upsw"),
				rs.getString("Urpsw"),
				rs.getString("Uage"),
				rs.getString("Uhp"),
				rs.getString("Usex"),
				rs.getString("Uhome"),
				rs.getString("Uexplain"),
				rs.getString("Ujst"));
			rs.close();//�رս����
			st.close();//�ر�ִ�п�
			conn.close();//�ر�����
			return x;//���ز�ѯ�����û���Ϣ
		}
		else {
			rs.close();//�رս����
			st.close();//�ر�ִ�п�
			conn.close();//�ر�����
			return null;
		}
	}
	public Integer Updateinventory(Integer Uid,String Uname,String Usex,String Uage,String Uemail,String Uhome) throws SQLException {
		//1����������
		Connection conn = getConn();
		//2�����������
		Statement st = conn.createStatement();
		//3����дsql
		String sql = "UPDATE  user set Uname = '"+Uname+"'"+",Usex='"+Usex+"'"+",Uage='"+Uage+"'"+",Uemail='"+Uemail+"'"+",Uhome='"+Uhome+"'"
				+" where Uid ='"+Uid+"'";
		Integer result = st.executeUpdate(sql);
		//5���ر�ִ�п�
		st.close();
		//6���ر�����
		conn.close();
		return result;
		}
	public Integer Updateinventory(Integer Uid,String Uexplain) throws SQLException {
		//1����������
		Connection conn = getConn();
		//2�����������
		Statement st = conn.createStatement();
		//3����дsql
		String sql = "UPDATE  user set Uexplain = '"+Uexplain+"'"+" where Uid ='"+Uid+"'";
		Integer result = st.executeUpdate(sql);
		//5���ر�ִ�п�
		st.close();
		//6���ر�����
		conn.close();
		return result;
		}
	
	
}
